package com.person.jc.web.test.assign;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.service.BsOrderService;
import com.person.jc.service.test.assign.BsTaskAssignService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;

/**
 * fcl维修任务分配
 */
public class BsTaskAssignController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		setAttr("startDate",DateUtils.formatDate(DateUtils.addMonths(new Date(), -1), "yyyy-MM-dd"));
		setAttr("endDate",DateUtils.getDate());
		String orderType = getPara("orderType");
		if("1".equals(orderType)){
			render("check/main.jsp");
		}else{
			render("repair/main.jsp");
		}
	}
	
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String orderType = String.valueOf(map.get("orderType"));
		if("1".equals(orderType)){
			List<Record> list = BsTaskAssignService.me.getList(map);
			setAttr("list", list);
			render("check/list.jsp");
		}else{
			List<Record> list = BsTaskAssignService.me.getList2(map);
			setAttr("list", list);
			render("repair/list.jsp");
		}
	}
	
	public void edit(){
		String orderId = getPara("orderId");
		String orderType = getPara("orderType");
		String type = getPara("type");
		String view = getPara("view");
		
		List<Record> list = BsTaskAssignService.me.getInfo(orderId, orderType, type);
		setAttr("list", list);
		if(list!=null && list.size()>0){
			Object assignId = (Object)list.get(0).get("assign_id");
			List<Record> leaderList = BsTaskAssignService.me.getLeaderList(assignId);
			setAttr("leaderList", leaderList);
		}
		setAttr("view", view);
		setAttr("type", type);
		
		if("1".equals(orderType)){
			render("check/edit.jsp");
		}else{
			render("repair/edit.jsp");
		}
	}
	
	@Before(Tx.class)
	public void save(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String assignId = String.valueOf(map.get("assignId"));
		String orderId = String.valueOf(map.get("orderId"));
		String userId1 = String.valueOf(map.get("userId1"));
		String userId2 = String.valueOf(map.get("userId2"));
		String userId3 = String.valueOf(map.get("userId3"));
		
		String type = getPara("type");
		String flow = Constants.FLOW_JCFP;
		if("wx".equals(type)){
			flow = Constants.FLOW_WX_WXFP;
			map.put("orderType", "3");
		}
		
		if(Utils.isEmpty(assignId)){
			assignId = BsTaskAssignService.me.save(map);
		}else{
			BsTaskAssignService.me.update(map);
			BsTaskAssignService.me.deleteUser(orderId);
		}
		BsTaskAssignService.me.saveUser(assignId, "1", userId1, orderId);
		BsTaskAssignService.me.saveUser(assignId, "2", userId2, orderId);
		BsTaskAssignService.me.saveUser(assignId, "3", userId3, orderId);

		//流程节点
		BsOrderService.me.updateOrderStatus(orderId, 2);
		BsOrderService.me.updateFlow(orderId, flow);
		
		renderText("success");
	}
	
	public void getUser(){
		String userId = getPara("userId");
		String orderType = getPara("orderType");
		String taskType = getPara("taskType");
		List<Record> list = BsTaskAssignService.me.getUser(userId);
		setAttr("list", list);
		setAttr("orderType", orderType);
		setAttr("taskType", taskType);
		render("check/user.jsp");
	}
	
	public void getTaskInfo(){
		TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
		Record record = BsTaskAssignService.me.getTaskInfo(tsUser.getUserId());
		setAttr("obj", record);
		render("task/task.jsp");
	}
}
